package org.lemon.cla.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; // 导入 UpdateWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.lemon.cla.pojo.Club;
import org.lemon.cla.pojo.User; // 导入 User 实体
import org.lemon.cla.mapper.ClubMapper;
import org.lemon.cla.service.IClubService;
import org.lemon.cla.service.IUserService; // 导入 IUserService，用于查询用户逻辑删除状态
import org.lemon.cla.dto.request.ClubSearchRequest;
import org.lemon.cla.enums.ClubStatusEnum;
import org.lemon.cla.vo.ClubVo; // 导入 ClubVo
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ClubServiceImpl extends ServiceImpl<ClubMapper, Club> implements IClubService {

    @Autowired
    private IUserService userService; // 注入 IUserService 用于查询用户状态

    // 辅助方法：处理 ClubVo 中的创建人信息
    private ClubVo processCreatorInfo(ClubVo clubVo) {
        if (clubVo == null) {
            return null;
        }
        // 如果 creatorId 为 null (ON DELETE SET NULL 发生)，或者 creatorIsDeleted 为 true
        if (clubVo.getCreatorId() == null || (clubVo.getCreatorIsDeleted() != null && clubVo.getCreatorIsDeleted())) {
            clubVo.setCreatorUsername("未知用户");
            clubVo.setCreatorAvatarUrl(null);
            clubVo.setCreatorRealName("未知");
            clubVo.setCreatorPhone(null);
            clubVo.setCreatorEmail(null);
            clubVo.setCreatorRole(null); // 或者设置为一个默认的未知角色枚举
        }
        return clubVo;
    }

    // 辅助方法：处理分页结果中的创建人信息
    private IPage<ClubVo> processCreatorInfoForPage(IPage<ClubVo> page) {
        if (page != null && page.getRecords() != null) {
            page.getRecords().forEach(this::processCreatorInfo);
        }
        return page;
    }

    @Override
    public Club getClubByClubName(String clubName) {
        QueryWrapper<Club> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("club_name", clubName);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean updateClubStatus(Long clubId, String newStatusStr) {
        ClubStatusEnum newStatus;
        try {
            newStatus = ClubStatusEnum.fromDescription(newStatusStr);
        } catch (IllegalArgumentException e) {
            return false;
        }

        if (baseMapper.selectCount(new QueryWrapper<Club>().eq("club_id", clubId)) == 0) {
            return false;
        }

        UpdateWrapper<Club> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("club_id", clubId)
                .set("status", newStatus); // 明确设置状态字段

        return baseMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public ClubVo getClubVoById(Long clubId) {
        ClubVo clubVo = baseMapper.selectClubVoById(clubId);
        return processCreatorInfo(clubVo); // 处理单个 ClubVo
    }

    @Override
    public IPage<ClubVo> getAllClubsVo(Page<ClubVo> page) {
        IPage<ClubVo> clubPage = baseMapper.selectClubVoPage(page);
        return processCreatorInfoForPage(clubPage); // 处理分页结果
    }

    @Override
    public IPage<ClubVo> searchClubsByConditions(Page<ClubVo> page, ClubSearchRequest request) {
        IPage<ClubVo> resultPage = baseMapper.selectClubVoByMultiConditions(page, request);
        return processCreatorInfoForPage(resultPage); // 处理分页结果
    }
}